<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Org\code\Code;
require_once app_path().'/Http/Org/code/Code.class.php';

use Hash;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Home\User;
use DB;
use Validator;
class LoginController extends Controller
{
    /*
     * 返回前台登录视图
     * @param
     * @return 登录视图
     * @author xxx
     * @date   2017/10/31  9:00
     */
    public function getLogin()
    {
        
        return view('home.login.login');

    }

    //返回验证码
    public function getCode()
    {
        $code = new Code;
        return $code->make();
    }

    //ajax验证用户账户密码是否一致
    public function getExists(Request $request)
    {
        $username=$request->input('username');
        $password=$request->input('password');
        $res=DB::table('home_user')->where('username',$username)->orWhere('phone',$username)->get();
        if (Hash::check($password,$res[0]->password)) {
            echo 1;
        }else{
           echo 2;
        }
    }
    //ajax验证用户是否存在
    public function getExist(Request $request)
    {
        $username=$request->input('username');
        $res=DB::table('home_user')->where('username',$username)->orWhere('phone',$username)->get();
        if ($res) {
            echo 1;
        }else{
           echo 2;
        }
    }

    //登录处理
    public function postDologin(Request $request)
    {
        //1. 接受用户提交的登录数据
        $input = $request->except('_token');
        if(strtoupper($input['code']) !=  session('code')){
            return redirect('/home/login/login')->with('errors','验证码错误');
        }
        $user = User::where('username',$input['username'])->first();
        //跳转到首页；如果登录失败,跳回到登录页
        session(['home_user'=>$user]);
        return redirect('home/index/index');
    }

    
}
